home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH7 / SRC / PANVIEW1.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1996-05-01  |  11.3 KB  |  364 lines

  1. VERSION 4.00
  2. Begin VB.Form ViewportForm 
  3.    Caption         =   "Viewport"
  4.    ClientHeight    =   3165
  5.    ClientLeft      =   2550
  6.    ClientTop       =   1800
  7.    ClientWidth     =   3150
  8.    Height          =   3855
  9.    Left            =   2490
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   3165
  12.    ScaleWidth      =   3150
  13.    Top             =   1170
  14.    Width           =   3270
  15.    Begin VB.HScrollBar HScrollBar 
  16.       Height          =   255
  17.       Left            =   0
  18.       TabIndex        =   2
  19.       Top             =   2880
  20.       Width           =   2895
  21.    End
  22.    Begin VB.VScrollBar VScrollBar 
  23.       Height          =   2895
  24.       Left            =   2880
  25.       TabIndex        =   1
  26.       Top             =   0
  27.       Width           =   255
  28.    End
  29.    Begin VB.PictureBox viewport 
  30.       Height          =   2880
  31.       Left            =   0
  32.       ScaleHeight     =   2820
  33.       ScaleWidth      =   2820
  34.       TabIndex        =   0
  35.       Top             =   0
  36.       Width           =   2880
  37.    End
  38.    Begin VB.Menu mnuFile 
  39.       Caption         =   "&File"
  40.       Begin VB.Menu mnuFileExit 
  41.          Caption         =   "E&xit"
  42.       End
  43.    End
  44.    Begin VB.Menu mnuScale 
  45.       Caption         =   "&Scale"
  46.       Begin VB.Menu mnuScaleMag 
  47.          Caption         =   "Full  Scale"
  48.          Index           =   1
  49.          Shortcut        =   ^F
  50.       End
  51.       Begin VB.Menu mnuScaleMag 
  52.          Caption         =   "Magnify &2"
  53.          Index           =   2
  54.          Shortcut        =   {F2}
  55.       End
  56.       Begin VB.Menu mnuScaleMag 
  57.          Caption         =   "Magnify &4"
  58.          Index           =   4
  59.          Shortcut        =   {F4}
  60.       End
  61.       Begin VB.Menu mnuScaleMag 
  62.          Caption         =   "Magnify 1/2"
  63.          Index           =   20
  64.          Shortcut        =   ^{F2}
  65.       End
  66.       Begin VB.Menu mnuScaleMag 
  67.          Caption         =   "Magnify 1/4"
  68.          Index           =   40
  69.          Shortcut        =   ^{F4}
  70.       End
  71.    End
  72. Attribute VB_Name = "ViewportForm"
  73. Attribute VB_Creatable = False
  74. Attribute VB_Exposed = False
  75. Option Explicit
  76. ' Global max and min world coordinates
  77. ' (including margins).
  78. Const DataXmin = 0
  79. Const DataXmax = 10
  80. Const DataYmin = 0
  81. Const DataYmax = 10
  82. ' Set the min and max allowed width and height.
  83. Const DataMinWid = 1
  84. Const DataMinHgt = 1
  85. Const DataMaxWid = DataXmax - DataXmin
  86. Const DataMaxHgt = DataYmax - DataYmin
  87. ' The aspect ratio of the viewport.
  88. Dim VAspect As Single
  89. ' Current world window bounds.
  90. Dim Wxmin As Single
  91. Dim Wxmax As Single
  92. Dim Wymin As Single
  93. Dim Wymax As Single
  94. ' Prevent change events when we are adjusting the
  95. ' scroll bars.
  96. Dim IgnoreSbarChange As Boolean
  97. ' ************************************************
  98. ' Adjust the world window so it is not too big,
  99. ' too small, off to one side, or of the wrong
  100. ' aspect ratio. Then map the world window to the
  101. ' viewport and force the viewport to repaint.
  102. ' ************************************************
  103. Sub SetWorldWindow()
  104. Dim wid As Single
  105. Dim hgt As Single
  106. Dim xmid As Single
  107. Dim ymid As Single
  108. Dim aspect As Single
  109.     wid = Wxmax - Wxmin
  110.     xmid = (Wxmax + Wxmin) / 2
  111.     hgt = Wymax - Wymin
  112.     ymid = (Wymax + Wymin) / 2
  113.         
  114.     ' Make sure we're not too big or too small.
  115.     If wid > DataMaxWid Then
  116.         wid = DataMaxWid
  117.     ElseIf wid < DataMinWid Then
  118.         wid = DataMinWid
  119.     End If
  120.     If hgt > DataMaxHgt Then
  121.         hgt = DataMaxHgt
  122.     ElseIf hgt < DataMinHgt Then
  123.         hgt = DataMinHgt
  124.     End If
  125.     ' Make the aspect ratio match the
  126.     ' viewport aspect ratio.
  127.     aspect = hgt / wid
  128.     If aspect > VAspect Then
  129.         ' Too tall and thin. Make it wider.
  130.         wid = hgt / VAspect
  131.     Else
  132.         ' Too short and wide. Make it taller.
  133.         hgt = wid * VAspect
  134.     End If
  135.     ' Compute the new coordinates
  136.     Wxmin = xmid - wid / 2
  137.     Wxmax = xmid + wid / 2
  138.     Wymin = ymid - hgt / 2
  139.     Wymax = ymid + hgt / 2
  140.     ' Check that we're not off to one side.
  141.     If wid > DataMaxWid Then
  142.         ' We're wider than the picture. Center.
  143.         xmid = (DataXmax + DataXmin) / 2
  144.         Wxmin = xmid - wid / 2
  145.         Wxmax = xmid + wid / 2
  146.     Else
  147.         ' Else see if we're too far to one side.
  148.         If Wxmin < DataXmin And Wxmax < DataXmax Then
  149.             ' Adjust to the right.
  150.             Wxmax = Wxmax + DataXmin - Wxmin
  151.             Wxmin = DataXmin
  152.         End If
  153.         If Wxmax > DataXmax And Wxmin > DataXmin Then
  154.             ' Adjust to the left.
  155.             Wxmin = Wxmin + DataXmax - Wxmax
  156.             Wxmax = DataXmax
  157.         End If
  158.     End If
  159.     If hgt > DataMaxHgt Then
  160.         ' We're taller than the picture. Center.
  161.         ymid = (DataYmax + DataYmin) / 2
  162.         Wymin = ymid - hgt / 2
  163.         Wymax = ymid + hgt / 2
  164.     Else
  165.         ' See if we're too far to top or bottom.
  166.         If Wymin < DataYmin And Wymax < DataYmax Then
  167.             ' Adjust downward.
  168.             Wymax = Wymax + DataYmin - Wymin
  169.             Wymin = DataYmin
  170.         End If
  171.         If Wymax > DataYmax And Wymin > DataYmin Then
  172.             ' Adjust upward.
  173.             Wymin = Wymin + DataYmax - Wymax
  174.             Wymax = DataYmax
  175.         End If
  176.     End If
  177.     ' Map the world window to the viewport.
  178.     Viewport.Scale (Wxmin, Wymax)-(Wxmax, Wymin)
  179.     ' Force the viewport to repaint.
  180.     Viewport.Refresh
  181.         
  182.     ' Reset the scroll bars.
  183.     IgnoreSbarChange = True
  184.     HScrollBar.Visible = (wid < DataXmax - DataXmin)
  185.     VScrollBar.Visible = (hgt < DataYmax - DataYmin)
  186.     ' The values of the scroll bars will be where
  187.     ' the top/left of the world window should be.
  188.     VScrollBar.Min = 100 * (DataYmax)
  189.     VScrollBar.Max = 100 * (DataYmin + hgt)
  190.     HScrollBar.Min = 100 * (DataXmin)
  191.     HScrollBar.Max = 100 * (DataXmax - wid)
  192.     ' SmallChange moves the world window 1/10
  193.     ' of its width/height. Large change moves it
  194.     ' 9/10 of its width/height.
  195.     VScrollBar.SmallChange = 100 * (hgt / 10)
  196.     VScrollBar.LargeChange = 100 * (9 * hgt / 10)
  197.     HScrollBar.SmallChange = 100 * (wid / 10)
  198.     HScrollBar.LargeChange = 100 * (9 * wid / 10)
  199.     ' Set the current scroll bar values.
  200.     VScrollBar.Value = 100 * Wymax
  201.     HScrollBar.Value = 100 * Wxmin
  202.     IgnoreSbarChange = False
  203. End Sub
  204. ' ************************************************
  205. ' Draw a smiley face in the viewport centered
  206. ' around the point (5, 5).
  207. ' ************************************************
  208. Sub DrawSmiley()
  209. Const pi = 3.14159265
  210. Const pi2 = 2 * pi
  211. Dim i As Single
  212.     Viewport.Circle (5, 5), 4            ' Head
  213.     Viewport.Circle (5, 5), 3, , pi, pi2 ' Smile
  214.     Viewport.Circle (3, 7), 0.75         ' Left eye.
  215.     Viewport.Circle (7, 7), 0.75         ' Right eye.
  216.     Viewport.Circle (5, 5), 0.75         ' Nose.
  217.     ' Draw some grid lines to make small scales
  218.     ' easier to understand.
  219.     i = DataXmin + 0.5
  220.     Do While i < DataXmax
  221.         Viewport.Line (i, DataYmin)-(i, DataYmax)
  222.         i = i + 0.5
  223.     Loop
  224.     i = DataYmin + 0.5
  225.     Do While i < DataYmax
  226.         Viewport.Line (DataXmin, i)-(DataXmax, i)
  227.         i = i + 0.5
  228.     Loop
  229. End Sub
  230. ' ************************************************
  231. ' Change the level of magnification.
  232. ' ************************************************
  233. Sub SetScaleFactor(fact As Single)
  234. Dim wid As Single
  235. Dim hgt As Single
  236. Dim mid As Single
  237.     fact = 1 / fact
  238.     ' Compute the new world window size.
  239.     wid = fact * (Wxmax - Wxmin)
  240.     hgt = fact * (Wymax - Wymin)
  241.     ' Center the new world window over the old.
  242.     mid = (Wxmax + Wxmin) / 2
  243.     Wxmin = mid - wid / 2
  244.     Wxmax = mid + wid / 2
  245.     mid = (Wymax + Wymin) / 2
  246.     Wymin = mid - hgt / 2
  247.     Wymax = mid + hgt / 2
  248.     ' Set the new world window bounds.
  249.     SetWorldWindow
  250. End Sub
  251. ' ************************************************
  252. ' Return to the default magnification scale.
  253. ' ************************************************
  254. Sub SetScaleFull()
  255.     ' Reset the world window coordinates.
  256.     Wxmin = DataXmin
  257.     Wxmax = DataXmax
  258.     Wymin = DataYmin
  259.     Wymax = DataYmax
  260.     ' Set the new world window bounds.
  261.     SetWorldWindow
  262. End Sub
  263. Private Sub Form_Resize()
  264. Dim x As Single
  265. Dim y As Single
  266. Dim wid As Single
  267. Dim hgt As Single
  268.     ' Fit the viewport to the window.
  269.     x = Viewport.Left
  270.     y = Viewport.Top
  271.     wid = ScaleWidth - 2 * x - VScrollBar.Width
  272.     hgt = ScaleHeight - 2 * y - HScrollBar.Height
  273.     Viewport.Move x, y, wid, hgt
  274.     VAspect = hgt / wid
  275.     ' Place the scroll bars next to the viewport.
  276.     x = Viewport.Left + Viewport.Width + 10
  277.     y = Viewport.Top
  278.     wid = VScrollBar.Width
  279.     hgt = Viewport.Height
  280.     VScrollBar.Move x, y, wid, hgt
  281.     x = Viewport.Left
  282.     y = Viewport.Top + Viewport.Height + 10
  283.     wid = Viewport.Width
  284.     hgt = HScrollBar.Height
  285.     HScrollBar.Move x, y, wid, hgt
  286.     ' Start at full scale.
  287.     SetScaleFull
  288. End Sub
  289. ' ************************************************
  290. ' Move the world window.
  291. ' ************************************************
  292. Private Sub HScrollBar_Change()
  293.     If IgnoreSbarChange Then Exit Sub
  294.     HScrollBarChanged
  295. End Sub
  296. ' ************************************************
  297. ' The vertical scroll bar has been moved. Adjust
  298. ' the world window.
  299. ' ************************************************
  300. Sub VScrollBarChanged()
  301. Dim hgt As Single
  302.     hgt = Wymax - Wymin
  303.     Wymax = VScrollBar.Value / 100
  304.     Wymin = Wymax - hgt
  305.     ' Remap the world window.
  306.     IgnoreSbarChange = True
  307.     SetWorldWindow
  308.     IgnoreSbarChange = False
  309. End Sub
  310. ' ************************************************
  311. ' The horizontal scroll bar has been moved. Adjust
  312. ' the world window.
  313. ' ************************************************
  314. Sub HScrollBarChanged()
  315. Dim wid As Single
  316.     wid = Wxmax - Wxmin
  317.     Wxmin = HScrollBar.Value / 100
  318.     Wxmax = Wxmin + wid
  319.     ' Remap the world window.
  320.     IgnoreSbarChange = True
  321.     SetWorldWindow
  322.     IgnoreSbarChange = False
  323. End Sub
  324. ' ************************************************
  325. ' Move the world window.
  326. ' ************************************************
  327. Private Sub HScrollBar_Scroll()
  328.     HScrollBarChanged
  329. End Sub
  330. Private Sub mnuFileExit_Click()
  331.     Unload Me
  332. End Sub
  333. ' ************************************************
  334. ' Change the level of magnification.
  335. ' ************************************************
  336. Private Sub mnuScaleMag_Click(Index As Integer)
  337.     If Index = 1 Then
  338.         ' Return to full scale.
  339.         SetScaleFull
  340.     ElseIf Index < 10 Then
  341.         ' Magnify by the indicated amount.
  342.         SetScaleFactor CSng(Index)
  343.     Else
  344.         ' Zoom out by 1/(Index \ 10).
  345.         SetScaleFactor 1 / (Index \ 10)
  346.     End If
  347. End Sub
  348. Private Sub Viewport_Paint()
  349.     DrawSmiley
  350. End Sub
  351. ' ************************************************
  352. ' Move the world window.
  353. ' ************************************************
  354. Private Sub VScrollBar_Change()
  355.     If IgnoreSbarChange Then Exit Sub
  356.     VScrollBarChanged
  357. End Sub
  358. ' ************************************************
  359. ' Move the world window.
  360. ' ************************************************
  361. Private Sub VScrollBar_Scroll()
  362.     VScrollBarChanged
  363. End Sub
  364.